package org.huawei260.OD260;//package org.huawei260;

import java.util.*;

public class O0015_第k长子串AI2 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		String s = scanner.nextLine();
		int k = scanner.nextInt();
		scanner.close();

		// 用于存储每个字母最长连续出现的次数
		Map<Character, Integer> map = new HashMap<>();

		// 用于记录当前字符和出现次数
		char currentChar = ' ';
		int currentCount = 0;
		int maxCount = 0;

		for (int i = 0; i < s.length(); i++) {
			if (s.charAt(i) == currentChar) {
				currentCount++;
			} else {
				if (currentChar != ' ') {
					// 更新之前字符的最长连续出现次数
					if (currentCount > maxCount) {
						maxCount = currentCount;
					}
					map.put(currentChar, maxCount);
				}
				currentChar = s.charAt(i);
				currentCount = 1;
			}
		}

		// 处理最后一个字符
		if (currentChar != ' ') {
			if (currentCount > maxCount) {
				maxCount = currentCount;
			}
			map.put(currentChar, maxCount);
		}

		// 将所有的最长连续出现次数放入一个列表中
		List<Integer> counts = new ArrayList<>(map.values());

		// 对列表进行排序
		Collections.sort(counts);

		// 判断是否存在第k长的子串
		if (k <= counts.size() && counts.size() > 0) {
			System.out.println(counts.get(counts.size() - k));
		} else {
			System.out.println(-1);
		}
	}
}